package com.xinqing.test.algorithm.dubbo;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.xinqing.test.modle.algorithm.ConvertMleapParam;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

/**
 * @Author:CHQ
 * @Date:2020/9/28 9:36
 * @Description
 */
public class ConvertMleap {
    public static void main(String args[]) throws Exception {
        String[] models={"/user/data_team/temp/zhijie/tfmodel/ssj_wdl_emb_v1/time_sparse_202009271825"};
        String hdfsDestPath="/user/data_team/temp/zhijie/tfmodel/ssj_wdl_emb_v1/time_sparse_202009271825.zip";

        ConvertMleapParam convertMleapParam = new ConvertMleapParam(models,hdfsDestPath);

        String data = JSON.toJSONString(convertMleapParam);

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

        String url = "http://sml.suishoudata.com/sml/model/convertMLEAP";

        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            System.out.println("rslt:" + response.getResponseBody());
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }

    }
}
